package com.lin.mq;

import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

/**
 * @program: blog
 * @description: MQ工具类
 * @author: Lin
 * @create: 2020-04-25 19:50
 **/
@Component
public class MQUtils {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 发送MQ消息
     * @param articleId
     * @param topic
     * @throws Exception
     */
    public void sendMQMessage(Long articleId,String topic) throws Exception {
        // 是否开启MQ
        if (MQConstant.MQ_IS_OPEN) {
            // 发送articleId
            Message message = MessageBuilder.withPayload(articleId).build();
            SendResult sendResult = rocketMQTemplate.syncSend(topic, message);
            if (SendStatus.SEND_OK.equals(sendResult.getSendStatus())){
            }else {
                throw new Exception("消息发送异常");
            }
        }
    }


}
